package com.jinbo.sxzq;

import android.util.Log;

/***********棋子羊********************/
public class Chess_Yang extends Chess{
    boolean flag;//是否触发了技能
    Chess_Yang(int type, int index, int[] location, int owner, GameControl gameControl) {
        /********初始化信息****************/
        super(index, location, owner, gameControl);
        this.type = type;
        this.name = ProjectConstants.ChessName.YANG;
        this.message = ProjectConstants.ChessMasage.YANG;
        this.type = ProjectConstants.ChessType.YANG;
        this.hp = 3;
        this.maxHp = 3;
        this.mp = 0;
        this.maxMp = 0;
        this.mobility = 2;
        this.maxmobility = 2;
        this.atk = 2;
        flag = false;
    }

    public void Action(){
        float minlength = 100;//离自己最近的敌方棋子的距离
        int aimindex = -1;//目标棋子的索引位置
        flag = false;
        if(this.mp >= this.maxMp){//触发技能
            Skill();
        }
        if(flag){
            return;
        }
        for(int i = 0 ; i < gameControl.chess_list.size() ; i++){//找到离自己最近的敌方棋子
            if(gameControl.chess_list.get(i).getOwner(this) != owner){
                Chess chess = gameControl.chess_list.get(i);
                float length = Math.max(Math.abs(chess.location[0]-location[0]),Math.abs(chess.location[1]-location[1]));
                if(minlength > length){
                    minlength = length;
                    aimindex = i;
                }
            }
        }
        if(aimindex != -1){
                Move(gameControl.chess_list.get(aimindex));
                Log.e("Action","移动");
        }else{
            Log.e("Action","未选中目标棋子");
            mobility--;
        }
    }

    public void Move(Chess chess){
        super.Move(chess);
    }

    public void Attack(Chess chess){
        super.Attack(chess);
    }

    public void RoundNext(){
        super.RoundNext();
    }

    //重写技能羊：【吸魂】无法攻击，攻击改为吸收距离1格的所有敌方棋子1能量，若敌方棋子无能量则吸收一点生命。
    public void Skill(){
        super.Skill();
        for(int i = 0 ; i < gameControl.chess_list.size() ; i++){
            if(gameControl.chess_list.get(i).owner != owner){
                Chess chess = gameControl.chess_list.get(i);
                float length = Math.max(chess.location[0]-location[0],chess.location[1]-location[1]);
                if(length == 1){
                    Log.e("Skill——羊" , "触发技能");
                    flag = true;
                    if(chess.mp > 0){
                        gameControl.chess_list.get(i).mp--;
                        mp++;
                    }else{
                        gameControl.chess_list.get(i).hp--;
                        hp++;
                        if(gameControl.chess_list.get(i).hp <= 0){
                            Kill(gameControl.chess_list.get(i));
                        }
                    }
                }
            }
        }
        if(flag){
            mobility--;
        }
    }


    public void Kill(Chess chess){
        super.Kill(chess);
    }
}
